from django.shortcuts import render
from django.http import HttpResponse, JsonResponse

# Create your views here.
from django.views.decorators.csrf import csrf_exempt

from Api.models import Book


def index(request):
    data = {
        'status': 200,
        'msg': 'ok',
    }

    return JsonResponse(data=data)


@csrf_exempt
def books(request):
    if request.method == 'GET':
        pass
    elif request.method == 'POST':
        print(request.POST)
        b_name = request.POST.get('b_name')
        b_price = request.POST.get('b_price')

        print(b_name)
        print(b_price)

        book = Book()
        book.b_name = b_name
        book.b_price = b_price

        book.save()

        data = {
            'status': 201,
            'msg': 'add success',
            'data': book.to_dict()
        }

        return JsonResponse(data=data)
